﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using PowerCreator.LMS.DL.Project.Entity;
using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.ViewQuery;
using PowerCreator.LMS.Web.Course;
public partial class sysadmin_DL_Project_ManageCourse : PowerCreator.LMS.Web.Base.WebBasePage
{
    protected Course opCourse = new Course();
    ViewQuery opViewQuery = new ViewQuery();
    PowerCreator.LMS.Web.Project.ProjectResource objProjectResource = new PowerCreator.LMS.Web.Project.ProjectResource();


    private string _SQL_Catalog_Course = "select "
        + "tc.Course_Id,tc.CourseName,tc.CourseMostlyTeacherName,tc.CourseIsCompulsory,tc.CourseState ,tc.CourseResourceNum"
        + " from TO_Course tc ";
    //private string _SQL_Catalog_Course = "select "
    //    + " tcc.CatalogcouID,tg.CatalogId,tg.CatalogName,tc.Course_Id,tc.CourseName,tc.CourseMostlyTeacherName,tc.CourseIsCompulsory,tc.CourseState ,tc.CourseResourceNum"
    //    + " from TCatalogCourse tcc left join TCatalog tg on tcc.FKCatalogcouCatalogID=tg.CatalogId "
    //    + " left join TO_Course tc on tcc.FKCatalogcouCourseID=tc.Course_Id ";

    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "../login.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //btnDel.Visible = IsLicensed("DelCourse");
            ViewState["ClassID"] = Request.QueryString["CID"].ToString(); ;  //0简单搜索1高级搜索
           
            InitPage();

            string search = GetSearch();
            BinderPager(search);
            BinderPG(search, 1);
        }
    }


    
    protected override void InitPage()
    {
        
        base.InitPage();

    }
    private string GetSearch()
    {
        StringBuilder sbSearch = new StringBuilder();
        sbSearch.Append(_SQL_Catalog_Course);

        //获取所有课程状态为有效的课程
        //sbSearch.Append(string.Format(" where CourseState = {0} and ",opCourse.GetCourseState(CourseState.Open)));
        string str_search = "select CourseID from TO_CourseList where CourseList_Id=(select ClassCourseListID from TO_Class where Class_Id={0})";
        str_search=string.Format(str_search,ViewState["ClassID"].ToString().Trim());
        string str_courseid = GetCourseIDlist(opViewQuery.Query(str_search).Rows[0][0].ToString().Trim());
        str_courseid = str_courseid.TrimEnd(',');
        sbSearch.Append(string.Format(" where Course_Id in ({0}) ", str_courseid));

        return sbSearch.ToString();
    }

    protected void BinderPager(string search)
    {
        AspNetPager1.RecordCount = opViewQuery.Count(search);
        AspNetPager1.PageSize = opViewQuery.Count(search);
    }
       
  
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPG(search, AspNetPager1.CurrentPageIndex);
    }

    private void BinderPG(string search, int pageIndex)
    {
        string orderList = " CourseID ";
        DataTable dt = opViewQuery.Query(search, LMSPageSize, pageIndex);
        rptNew.DataSource = dt;
        rptNew.DataBind();
        PreAspNetPager(AspNetPager1);
    }

    protected void rptNew_ItemCommand(object soure, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToString() == "jsyt")
        {
            string url = string.Format("ManageResource.aspx?Type=1&DomainID={0}&ClassID={1}", e.CommandArgument.ToString(), ViewState["ClassID"].ToString());
            Script.RedirectURL(url);
        }
        else if (e.CommandName.ToString() == "pjgl")
        {
            string url = string.Format("../../CourseManager/ManageCoursePinion.aspx?type=0&CourseID={0}", e.CommandArgument.ToString());
            Script.RedirectURL(url);
        }
        else if (e.CommandName.ToString() == "csjpy")
        {
            string url = string.Format("../../PaperManager/TestPaperUserList.aspx?CourseID={0}", e.CommandArgument.ToString());
            Script.RedirectURL(url);
        }
    }

    private string GetCourseIDlist(string val)
    {
        string rt = "0";
         string[] CourseIDsList = val.Split(',');
         for (int i = 0; i < CourseIDsList.Length; i++)
         {
             string[] par = CourseIDsList[i].Split('#');
             rt = rt + "," + par[0];
         }
         return rt;


    }
   
 
}


